//
//  ZYEmoticonTools.swift
//  WBBeta1
//
//  Created by wustzhy on 16/8/19.
//  Copyright © 2016年 Yestin. All rights reserved.
//

import UIKit

// 最多显示的列数 和 行数 和最多显示表情个数
let ZYEmoticonsMaxCol = 7
let ZYEmoticonsMaxRow = 3
let ZYEmoticonsMaxCount = ZYEmoticonsMaxCol*ZYEmoticonsMaxRow - 1

class ZYEmoticonTools: NSObject {

    //单例
    static let sharedTools:ZYEmoticonTools = ZYEmoticonTools()
    
    //emoticonsBundle
    lazy var emoticonsBundle:NSBundle = {
       let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)
        return NSBundle(path: path!)!
    }()
    
//    /Users/wustzhy/Desktop/oschinaProj/framework01/WBBeta1/WBBeta1/Classes/Compose/view/Emoticon/Emoticons.bundle/Contents/Resources/emoji/info.plist
    func getEmoticonsWithPath(path:String) -> [ZYEmoticonModel]{
        let file = emoticonsBundle.pathForResource("\(path)/info.plist", ofType: nil)
        let array = NSArray(contentsOfFile: file!)
        var tempArr = [ZYEmoticonModel]()
        for dic in array! {
            let model = ZYEmoticonModel(dit: dic as! [String : AnyObject])
            
            //记录path 获取图片时需要拼接
            model.path = "\(path)/"
            //default/d_aini@2x.png
            
            tempArr.append(model)
            
        }

        
        return tempArr
    }
    
    
//MARK: 表情 一维数组
    lazy var defaultEmoticons:[ZYEmoticonModel] = {
       return self.getEmoticonsWithPath("default")  //self 不可缺 failed
    }()
    lazy var emojiEmoticons:[ZYEmoticonModel] = {
        return self.getEmoticonsWithPath("emoji")  //self 不可缺 failed
    }()
    lazy var lxhEmoticons:[ZYEmoticonModel] = {
        return self.getEmoticonsWithPath("lxh")  //self 不可缺 failed
    }()
    
//MARK: 二维数组 == 一维数组分N页（每页20个表情）
    func getEmotionGroupsWithEmoticons(emoticons: [ZYEmoticonModel]) -> [[ZYEmoticonModel]] {
        print("0000000")
        print(emoticons.count)
        //计算出--页数
        let pageCount = (emoticons.count - 1)/ZYEmoticonsMaxCount + 1
        //创建临时二维数组
        var tempArr:[[ZYEmoticonModel]] = [[ZYEmoticonModel]]()
        for i in 0..<pageCount {
            let location = i * ZYEmoticonsMaxCount
            var length = ZYEmoticonsMaxCount
            // 越界判断
            if location + length > emoticons.count {
                length = emoticons.count - location
            }
            let rang = NSRange(location: location, length: length)
            
            
            
            //使用OC的方法，需要转为NSArray
            let array = (emoticons as NSArray).subarrayWithRange(rang) as! [ZYEmoticonModel]
            tempArr.append(array)
        }
        
        return tempArr
    }
    
//MARK: 三维数组    
    lazy var allEmoticons:[[[ZYEmoticonModel]]] = {
       return [
            self.getEmotionGroupsWithEmoticons(self.defaultEmoticons),
            self.getEmotionGroupsWithEmoticons(self.emojiEmoticons),
            self.getEmotionGroupsWithEmoticons(self.lxhEmoticons)
        ]
    }()
    
    
    
    
}
